<?php

namespace StudyBuddy\Controllers;

use StudyBuddy\SubmittedProfileWWW;
use StudyBuddy\WebPage;
use StudyBuddy\ProfileParser;
use StudyBuddy\Responder;

class Editprofile extends WebPage
{
	/**
	 * Pre-check to deny non-logged in user
	 * access to this page
	 *
	 * @var bool
	 */
	protected $membersOnly = true;


	/**
	 *
	 * Viewer must have edit_profile
	 * permission to access this page
	 * @var string
	 */
	protected $permission = 'edit_profile';


	/**
	 * $layoutID 1 means no side-column on page
	 *
	 * @var int
	 */
	protected $layoutID = 1;


	/**
	 * Form object
	 *
	 * @var object of type \StudyBuddy\Forms\Form
	 */
	protected $oForm;


	/**
	 * User object of user whose profile
	 * is being edited
	 *
	 * @var object of type User
	 */
	protected $oUser;


	protected function main(){
		$this->getUser();
                if(true) {
                    $this->oForm = new \StudyBuddy\Forms\Profile($this->oRegistry);
                } else {
                    $this->oForm = new \StudyBuddy\Forms\Tutor($this->oRegistry);
                }
		$this->oForm = new \StudyBuddy\Forms\Profile($this->oRegistry);
		$this->oForm->formTitle = $this->aPageVars['title'] = $this->_('Edit Profile');

		if($this->oForm->isSubmitted() && $this->oForm->validate()){
			$this->oRegistry->Dispatcher->post($this->oForm, 'onBeforeProfileUpdate');
			//try{
				$this->saveProfile();
				$this->oRegistry->Dispatcher->post($this->oForm, 'onProfileUpdate');
				$this->aPageVars['body'] = \tplProfileSuccess::parse(array('Profile has been updated', $this->oUser->getProfileUrl(), 'View the new profile'), false);
				Responder::redirectToPage('/users/' . $this->oRequest->get('uid', 'i', null) . '/' . $this->oRequest->get('username'));
			/*} catch (\StudyBuddy\Exception $e){
				$this->oForm->setFormError($e->getMessage());
				$this->setForm();
				$this->aPageVars['body'] = $this->oForm->getForm();
			}*/
		} else {
			$this->setForm();
			$this->aPageVars['body'] = $this->oForm->getForm();
		}
	}


	/**
	 * Create $this->oUser User object for user whose
	 * profile is being edited
	 *
	 * @todo unfinished. IT will be possible to
	 * edit user other than Viewer when Viewer has
	 * permission to edit_other_profile
	 * For now this is a Viewe object
	 *
	 * @return object $this
	 */
	protected function getUser(){
		$uid = $this->oRequest->get('uid', 'i', null);
		if($uid && ($uid !== $this->oRegistry->Viewer->getUid())){
			/**
			 * This is edit profile for another user
			 * check Viewer permission here
			 */
			$this->checkAccessPermission('edit_any_profile');
			$this->oUser = \StudyBuddy\User::factory($this->oRegistry)->by_id($uid);
		} else {
			$this->oUser = $this->oRegistry->Viewer;
		}

		return $this;
	}


	/**
	 * Populate form elements with
	 * values from current user profile
	 *
	 * @return object $this
	 */
	protected function setForm(){

                if(isset($this->oRegistry->Viewer['tmp_role']) && $this->oRegistry->Viewer['tmp_role'] == 'tutor') {
                    $this->oForm->js_redirect = '<script type="text/javascript">window.location=\'http://'.$_SERVER['HTTP_HOST'].'/regtutor/\';</script>';
                } 
		$this->oForm->tmp_role = $this->getRoleInput();
		$this->oForm->tmp_role_display = $this->getRoleInput1();
		$this->oForm->username = $this->oUser['username'];
		$this->oForm->usernameLabel = 'Username';
		$this->oForm->fn = $this->oUser['fn'];
		$this->oForm->mn = $this->oUser['mn'];
		$this->oForm->ln = $this->oUser['ln'];
		$this->oForm->fullname = $this->oUser['fullname'];
		$this->oForm->gender = $this->getGenderOptions();
		$this->oForm->dob = $this->oUser['dob'];
		$this->oForm->cc = $this->getCountryOptions();
		$this->oForm->state = $this->oUser['state'];
		$this->oForm->city = $this->oUser['city'];
		$this->oForm->url = $this->oUser['url'];
		$this->oForm->zip = $this->oUser['zip'];
		$this->oForm->description = $this->oUser['description'];
		$this->oForm->sowa = $this->oUser['sowa'];
		$this->oForm->avatarSrc = $this->oUser->getAvatarSrc();
		$this->oForm->width = $this->oRegistry->Ini->AVATAR_SQUARE_SIZE;
		$this->oForm->uid = $this->oUser->getUid();
		$this->oForm->maxAvatarSize = $this->oRegistry->Ini->MAX_AVATAR_UPLOAD_SIZE;
		/**
		 * @todo translate string
		 */
		$this->oForm->avatarTos = sprintf('Upload Image. Maximum size of %sMb<br><span class="smaller">By uploading a file you certify that you have the right to distribute this picture and that it does not violate the Terms of Service.</span>', floor($this->oRegistry->Ini->MAX_AVATAR_UPLOAD_SIZE / 1000000) );

		/**
		 * Add '  hide' class to avatar upload
		 * if php does not have gd of support for jpeg
		 * inside gd
		 */
		if(!\extension_loaded('gd') || !\function_exists('imagecreatefromjpeg')){
			$this->oForm->hideAvatar = ' hide';
		}

		return $this;
	}


	/**
	 * Save changed to user profile
	 * using ProfileParser class
	 *
	 * @return object $this
	 */
	protected function saveProfile(){

		ProfileParser::factory($this->oRegistry)->save($this->oUser, new SubmittedProfileWWW($this->oForm));

		/**
		 * Should unset 'welcome' from session
		 * because it contains user display name in the
		 * link and display name may have changed as a
		 * result of editing first, middle and last names
		 */
		unset($_SESSION['welcome']);

		return $this;
	}


	/**
	 * Generates string with <option></option> html elements
	 * that will be used as html of the <select> dropdown
	 * menu for Country selection
	 * 
	 * @todo use 'cc' key from USER as value
	 * and country name only as label! Don NOT
	 * set 'country' key at all!
	 * For this we need an array that translates
	 * 2-letter country code 'cc' to the full country name
	 *
	 * @return string html string
	 */
	protected function getCountryOptions(){
		$s = '';
		$current = \strtoupper($this->oRegistry->Viewer['cc']);
		$tpl = '<option value="%1$s"%2$s>%3$s</option>';
		$aCountries = \array_combine(\StudyBuddy\Geo\Location::getCodes(), \StudyBuddy\Geo\Location::getNames());
		
		foreach($aCountries as $key => $val){
			$selected = ($current == $key) ? ' selected' : '';
			$name = (empty($val)) ? 'Select country' : $val;

			$s .= \vsprintf($tpl, array($key, $selected, $name));
		}

		return $s;
	}


	/**
	 * Generates string with 3 <option></option> html elements
	 * for the value of "Gender" drop-down menu
	 * it also sets the "selected" value of an option
	 * that matches the current value in Viewer object
	 *
	 * @todo translate string Male and Female but not the values "M" and "F"
	 *
	 * @return string html string
	 */
	protected function getGenderOptions(){
		$current = $this->oRegistry->Viewer['gender'];
		$s = '';
		$a = array('' => 'Select Gender', 'M' => 'Male', 'F' => 'Female');
		$tpl = '<option value="%1$s"%2$s>%3$s</option>';
		foreach($a as $key => $val){
			$selected = ($key == $current) ? ' selected' : '';
			$s .= vsprintf($tpl, array($key, $selected, $val));
		}

		return $s;
	}
        
        protected function getRoleInput(){
            $current = $this->oRegistry->Viewer['role'];
            $current = ($current != 'parent' && $current != 'tutor') ? 'student' : $current;
            $s = '';
            $a = array('student' => 'Student', 'parent' => 'Parent', 'tutor' => 'Tutor');
            $tpl = '<input type="radio" id="id_role%1$s" name="tmp_role" readonly="readonly" value="%2$s"%3$s%4$s /> %5$s ';
            $i = 1;
            foreach ($a as $key => $value){
                $checked = ($key == $current) ? ' checked="checked"' : '';
                $tutor = ($key == 'tutor') ? ' onclick="window.location=\'http://'.$_SERVER['HTTP_HOST'].'/regtutor\'"' : '';
                $s .= vsprintf($tpl, array($i, $key, $checked, $tutor, $value));
                $i++;
            }
            return $s;
        }
		
		protected function getRoleInput1(){
            $current = $this->oRegistry->Viewer['role'];
            $current = ($current != 'parent' && $current != 'tutor') ? 'student' : $current;
            return strtoupper($current);
        }

}
